<!DOCTYPE html>
<html>
<!--
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
<title>Transform</title>
<script src="include.js"></script>
<style>
body, html {
  margin: 0;
  padding: 0;
}

textarea {
  width: 100%;
  height: 100%;
  margin: 0;
  border: 0;
  padding: 0;
  resize: none;
}

.box {
  font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Lucida Console', monospace;
  font-size: 80%;
}

.container {
  position: absolute;
  width: 50%;
  height: 50%;
}

.container > * {
  position: absolute;
  border: 1px solid black;
  margin: 0;
  top: 10px;
  left: 10px;
  bottom: 10px;
  right: 10px;
}

#tl {
  top: 0;
  left: 0;
}

#tr {
  top: 0;
  right: 0;
}

#bl {
  bottom: 0;
  left: 0;
}

#br {
  bottom: 0;
  right: 0;
}

.placeholder {
  color: grey;
}
</style>
<script>
var m;
var markup;
var data;
var markupElm;
var dataElm;
var outputElm;
var exampleElm;

window.onload = function() {
  markupElm = document.getElementById('markup');
  dataElm = document.getElementById('data');
  outputElm = document.getElementById('output');
  exampleElm = document.getElementById('example');

  var query = decodeQuery(getFragment());
  markupElm.value = query.markup || '';
  dataElm.value = query.data || '';
  if (markupElm.value)
    update();
}

function reval() {
  setTimeout(update, 0);
}

var forEach = Array.prototype.forEach.call.bind(Array.prototype.forEach);

function update() {
  markup = markupElm.value;
  data = dataElm.value;

  var query = {};
  query.markup = markup;
  query.data = data;
  setFragment(encodeQuery(query));

  exampleElm.innerHTML = markup;

  try {
    eval('m = exampleElm.model = ' + data + ';');
    var templates = exampleElm.querySelectorAll('template');
    forEach(templates, HTMLTemplateElement.decorate);
    outputElm.innerHTML = escapeHTML(exampleElm.innerHTML);
  } catch (ex) {
    exampleElm.innerHTML = ex;
  }
};

function setFragment(val) {
  var href = [
    location.protocol,
    '//',
    location.hostname,
    location.port ? ':' + location.port : '',
    location.pathname,
    location.search,
    '#',
    val ? val : ''];
  location.replace(href.join(''));
};

function getFragment() {
  var hashIndex = window.location.href.indexOf("#");
  if (hashIndex == -1) return '';
  return window.location.href.substr(hashIndex + 1);
};

function encodeQuery(map) {
  var q = [];
  for (var key in map) {
    q.push(encodeURIComponent(key) + '=' +
        encodeURIComponent(map[key]));
  }
  return q.join('&');
};

function decodeQuery(q) {
  var map = {};
  var pairs = q.split('&');
  for (var i = 0; i < pairs.length; i++) {
    var pair = pairs[i].split('=');
    var key = decodeURIComponent(pair[0]);
    var val = decodeURIComponent(pair[1]);
    map[key] = val;
  }
  return map;
};

function unescapeHTML(html) {
  return html.replace(/&lt;/g, '<')
             .replace(/&gt;/g, '>')
             .replace(/&nbsp;/g, ' ');
}

function escapeHTML(raw) {
  return raw.replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/ /g, '&nbsp;');
}

</script>
</head>
<body>
  <div id="tl" class="container">
    <div>
      <textarea id="markup" onkeydown="reval();" class="box"
                placeholder="Write your HTML view here..."></textarea>
    </div>
  </div>

  <div id="tr" class="container">
    <div>
      <textarea id="data" onkeydown="reval();" class="box"
                placeholder="...and your JSON model here!"></textarea>
    </div>
  </div>

  <div id="bl" class="container">
    <div id="example" class="box">
      <span class="placeholder">Formatted output will appear here...</span>
    </div>
  </div>

  <div id="br" class="container">
    <pre id="output" class="box"><span class="placeholder">...and generated HTML source here!</span></pre>
  </div>
</div>

</body>
</html>
